package com.itsv.cyzx.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.itsv.cyzx.bean.ContactsBanBen;
import com.itsv.cyzx.bean.ContactsBean;

public class DBHelperCon extends SQLiteOpenHelper {

	SQLiteDatabase db = null;
	private static final int dbVersion = 2;
	private Context context = null;
	private static String dbname = "newcontacts.db";

	public DBHelperCon(Context context) {
		super(context, dbname, null, dbVersion);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 创建联系人表
		String tablesql = "create table Tab_Contacts(id varchar(20),phonenum varchar(20),imei varchar(20),name varchar(20),sex varchar(20),zerendanwei varchar(20),email varchar(20),"
				+ "addres varchar(20),picture varchar(20),isLine varchar(20),subsector varchar(20),temp varchar(20),temp1 varchar(20),temp2 varchar(20),"
				+ "duties varchar(20),party varchar(20),sector varchar(20),unitsuperior integer, wyhmc varchar(20),wybh varchar(20))";
		db.execSQL(tablesql);
		// 创建联系人版本表
		tablesql = "create table Tab_ContactsBanBen(version varchar(20))";
		db.execSQL(tablesql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (newVersion <= oldVersion) {
			return;
		} else {
			context.deleteDatabase(dbname);
			onCreate(db);
		}
	}

	/**
	 * 获取联系人列表
	 * 
	 * @return
	 */
	public ArrayList<ContactsBean> getContactsList() {
		ArrayList<ContactsBean> list = new ArrayList<ContactsBean>();
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		Cursor cursor = null;
		String sql = "select * from Tab_Contacts";
		cursor = db.rawQuery(sql, null);
		ContactsBean bean = null;

		while (cursor.moveToNext()) {
			bean = new ContactsBean();
			bean.setId(cursor.getString(cursor.getColumnIndex("id")));
			bean.setPhonenum(cursor.getString(cursor.getColumnIndex("phonenum")));
			bean.setImei(cursor.getString(cursor.getColumnIndex("imei")));
			bean.setName(cursor.getString(cursor.getColumnIndex("name")));
			bean.setSex(cursor.getString(cursor.getColumnIndex("sex")));
			bean.setZerendanwei(cursor.getString(cursor
					.getColumnIndex("zerendanwei")));
			bean.setEmail(cursor.getString(cursor.getColumnIndex("email")));
			bean.setAddres(cursor.getString(cursor.getColumnIndex("addres")));
			bean.setPicture(cursor.getString(cursor.getColumnIndex("picture")));
			bean.setIsLine(cursor.getString(cursor.getColumnIndex("isLine")));
			bean.setSubsector(cursor.getString(cursor
					.getColumnIndex("subsector")));
			bean.setTemp(cursor.getString(cursor.getColumnIndex("temp")));
			bean.setTemp1(cursor.getString(cursor.getColumnIndex("temp1")));
			bean.setTemp2(cursor.getString(cursor.getColumnIndex("temp2")));
			bean.setDuties(cursor.getString(cursor.getColumnIndex("duties")));
			bean.setParty(cursor.getString(cursor.getColumnIndex("party")));
			bean.setSector(cursor.getString(cursor.getColumnIndex("sector")));
			bean.setUnitsuperior(cursor.getInt(cursor
					.getColumnIndex("unitsuperior")));
			bean.setWyhmc(cursor.getString(cursor
					.getColumnIndex("wyhmc")));
			bean.setWybh(cursor.getString(cursor
					.getColumnIndex("wybh")));
			list.add(bean);
		}
		cursor.close();
		return list;
	}

	/**
	 * 添加联系人信息
	 * 
	 * @param contactsBean
	 */
	public void setContacts(ContactsBean contactsBean) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		// 删除同一id的联系人信息
		String id = contactsBean.getId();
		db.delete("Tab_Contacts", "id=?", new String[] { id });
		ContentValues values = new ContentValues();
		values.put("id", contactsBean.getId());
		values.put("phonenum", contactsBean.getPhonenum());
		values.put("imei", contactsBean.getImei());
		values.put("name", contactsBean.getName());
		values.put("sex", contactsBean.getSex());
		values.put("zerendanwei", contactsBean.getZerendanwei());
		values.put("email", contactsBean.getEmail());
		values.put("addres", contactsBean.getAddres());
		values.put("picture", contactsBean.getPicture());
		values.put("isLine", contactsBean.getIsLine());
		values.put("subsector", contactsBean.getSubsector());
		values.put("temp", contactsBean.getTemp());
		values.put("temp1", contactsBean.getTemp1());
		values.put("temp2", contactsBean.getTemp2());
		values.put("duties", contactsBean.getDuties());
		values.put("party", contactsBean.getParty());
		values.put("sector", contactsBean.getSector());
		values.put("unitsuperior", contactsBean.getUnitsuperior());
		values.put("wybh", contactsBean.getWybh());
		values.put("wyhmc", contactsBean.getWyhmc());
		db.insert("Tab_Contacts", null, values);
	}

	public void update(String id, String temp1) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		ContentValues values = new ContentValues();
		values.put("temp1", temp1);
		db.update("Tab_Contacts", values, "id=?", new String[] { id });
		db.close();
	}

	/**
	 * 获取联系人版本
	 * 
	 * @return
	 */
	public String getContactsBanBen() {
		String banben = null;
		ContactsBanBen bean = new ContactsBanBen();
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		Cursor cursor = null;
		String sql = "select * from Tab_ContactsBanBen";
		cursor = db.rawQuery(sql, null);

		while (cursor.moveToNext()) {
			bean.setVersion(cursor.getString(cursor.getColumnIndex("version")));
		}
		banben = bean.getVersion();
		cursor.close();
		return banben;
	}

	/**
	 * 添加联系人版本
	 * 
	 * @param contactsBean
	 */
	public void setContactsBanBen(String banben) {
		if (db == null || !db.isOpen()) {
			db = getWritableDatabase();
		}
		// 删除联系人版本信息
		db.delete("Tab_ContactsBanBen", null, null);
		ContentValues values = new ContentValues();
		values.put("version", banben);
		db.insert("Tab_ContactsBanBen", null, values);
	}
}
